package com.imaker.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imaker.dao.BaseDao;
import com.imaker.domain.ActionOperateInfo;
import com.imaker.domain.OperateInfo;
import com.imaker.domain.Page;
import com.imaker.field.StaticField;

 /**
 * <p>程序名称：       OperateService.java</p>
 * <p>程序说明：       TODO</p>
 * <p>版权信息：       Copyright mayonghua</p>
 * 
 * @author：          mayonghua
 * @version：         Ver 0.1
 */
@Service
public class ActionOperateService implements IService {
	private BaseDao<ActionOperateInfo> actionOperateDAO = null;
	private BaseDao<OperateInfo> operateDAO = null;
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		actionOperateDAO = new BaseDao<ActionOperateInfo>(sessionFactory,ActionOperateInfo.class);
		operateDAO = new BaseDao<OperateInfo>(sessionFactory,OperateInfo.class);
	}
	
	public String save(ActionOperateInfo actionOperateInfo){
		try{
			actionOperateDAO.save(actionOperateInfo);	
		}catch(Exception e){
			e.printStackTrace();
			return StaticField.error;
		}
		return StaticField.success;
	}
	
	/**
	 * 查询所有
	 * @return
	 * @throws Exception
	 */
	public List<ActionOperateInfo> findAll() throws Exception{
		return actionOperateDAO.findAll();
	}
	
	/**
	 * 根据ID进行查找
	 * @param actionOperateInfo
	 * @return
	 * @throws Exception
	 */
	public ActionOperateInfo findById(String id) throws Exception{
		return actionOperateDAO.get(id);
	}
	
	/**
	 * 删除
	 */
	public String delete(List<String> idList){
		try {
			for(String id:idList){
				ActionOperateInfo aoi = actionOperateDAO.get(id);
				actionOperateDAO.delete(aoi.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return StaticField.error;
		}
		return StaticField.success;
	}
	
	/**
	 * 更新
	 */
	public String update(ActionOperateInfo actionOperateInfo){
		try {
			actionOperateDAO.update(actionOperateInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return StaticField.error;
		}
		return StaticField.success;
	}
	
	/**
	 * 分页查找
	 */
	public Page listByPage(Page page) throws Exception{
		String hql = "from ActionOperateInfo a";
		return actionOperateDAO.pagination(page, hql,"");
	}
	
	/**
	 * 分页查
	 */
	public Page listByPage(Page page,String id) throws Exception{
		String hql = "from ActionOperateInfo a where a.action.id = ?";
		return actionOperateDAO.pagination(page, hql,"",id);
	}
}
